﻿using Acme.BookStore2.Permissions;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;

namespace Acme.BookStore2
{
    [Authorize(BookStore2Permissions.SecurityLog.Default)]
    public class SecurityLogAppService : BookStore2AppService, ISecurityLogAppService
    {
        private readonly IIdentitySecurityLogRepository _identitySecurityLogRepository;

        public SecurityLogAppService(IIdentitySecurityLogRepository identitySecurityLogRepository)
        {
            _identitySecurityLogRepository = identitySecurityLogRepository;
        }

        [Authorize(BookStore2Permissions.SecurityLog.List)]
        public async Task<PagedResultDto<SecurityLogDto>> GetListAsync(SecurityLogPagedAndSortedResultRequestDto input)
        {
            var list = await _identitySecurityLogRepository.GetListAsync(
                sorting: input.Sorting,
                maxResultCount: input.MaxResultCount,
                skipCount: input.SkipCount,
                startTime: input.StartTime,
                endTime: input.EndTime,
                applicationName: input.ApplicationName,
                identity: input.Identity,
                action: input.ActionName,
                userId: input.UserId,
                userName: input.UserName,
                clientId: input.ClientId,
                correlationId: input.CorrelationId);
            var totalCount = await _identitySecurityLogRepository.GetCountAsync();

            return new PagedResultDto<SecurityLogDto>(
                totalCount,
                ObjectMapper.Map<List<IdentitySecurityLog>, List<SecurityLogDto>>(list)
                );
        }
    }
}
